<template>
  <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @confirm="onConfirm"
      @cancel="$emit('onCancel')"
  />
</template>

<script>
import { editUserProfile } from '@/api/user'
import { Toast } from 'vant'

export default {
  name: 'EditGender',
  props: {
    value: {
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  created () {

  },
  methods: {
    async onConfirm (value, index) {
      if (index === this.value) return Toast('您已经是这个性别了呢')
      console.log('确认')
      try {
        await editUserProfile({
          gender: index
        })
        Toast('更新成功')
        this.$emit('input', index)

        this.$emit('onCancel')
      } catch (e) {
        console.dir(e)
        Toast('更新失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
